// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using Byond.EnsembleLibrary;
using Mvvm;
using System.Linq;

namespace Byond.Frontend.EnsembleBrowserNew.ViewModels
{
	public class FolderViewModel : ViewModelBase
	{
		private readonly LibraryFolder _libraryFolder;
		private readonly ItemSelectionArbiter _itemSelectionArbiter;
		private readonly EnsembleAdministrationWorkflows _ensembleAdministrationWorkflows;
		private readonly Func<LibraryItem, FolderViewModel, ItemViewModel> _itemFactory;
		private readonly Func<LibraryFolder, FolderViewModel> _folderFactory;

		public FolderViewModel(LibraryFolder libraryFolder)
		{
			_libraryFolder = libraryFolder;
		}

		private void Ctor()
		{
			Refresh();
		}

		public string FolderName { get { return _libraryFolder.Name; } }
		public string Path { get { return _libraryFolder.Path; } }

		private readonly ObservableCollection<object> _children = new ObservableCollection<object>();
		public IEnumerable<object> Children { get { return _children; } }

		public FolderViewModel Parent { get; set; }

		private bool _isExpanded;
		public bool IsExpanded
		{
			get { return _isExpanded; }
			private set
			{
				_isExpanded = value;
				OnPropertyChanged("IsExpanded");
			}
		}

		private bool _isPopupOpen;
		public bool IsPopupOpen
		{
			get { return _isPopupOpen; }
			set
			{
				_isPopupOpen = value;
				OnPropertyChanged("IsPopupOpen");
			}
		}

		public Visibility RefreshVisibility
		{
			get
			{
				return _libraryFolder.IsRoot ? Visibility.Visible : Visibility.Collapsed;
			}
		}

		public IEnumerable<FolderViewModel> Flatten()
		{
			var list = new List<FolderViewModel>();
			DoFlatten(list);

			return list;
		}

		private void DoFlatten(ICollection<FolderViewModel> list)
		{
			list.Add(this);
			foreach (var it in _children.OfType<FolderViewModel>())
				it.DoFlatten(list);
		}

		public void ToggleExpansion()
		{
			IsExpanded = !IsExpanded;
		}

		public void Refresh()
		{
			_libraryFolder.Refresh();

			_children.Clear();

			foreach (var folder in _libraryFolder.Folders)
			{
				var folderViewModel = _folderFactory(folder);
				folderViewModel.Parent = this;

				_children.Add(folderViewModel);
			}

			foreach (var item in _libraryFolder.Items)
				_children.Add(_itemFactory(item, this));
		}

		public void NewFolder()
		{
			_ensembleAdministrationWorkflows.NewFolder(Path);
			IsPopupOpen = false;
		}

		public void NewItem()
		{
			_ensembleAdministrationWorkflows.NewItem(Path);
			IsPopupOpen = false;
		}

		public void Expand()
		{
			if (IsExpanded)
				return;
			ToggleExpansion();
		}

		public void ExpandTree()
		{
			Expand();
			if (Parent != null)
				Parent.ExpandTree();
		}

		public void OpenPopup()
		{
			IsPopupOpen = true;
		}
	}
}